using System;
using System.Collections.Generic;
using Animancer;
using Config;
using MoonFramework.Scripts.View.Music;
using UnityEngine;

namespace Entity
{
    public abstract class ActorViewController : MonoBehaviour
    {
        protected Vector2 posXScope; //x的范围
        protected Vector2 posZScope; //z的范围
        public Vector3 targetDir;

        #region 组件
        [HideInInspector] public ClipTransition _AttackAnimation ,_IdleAnimation, _RunAnimation, _DeadAnimation, _WalkAnimation, _HurtAnimation;
        [HideInInspector] public AnimancerComponent AnimancerComponent;
        public Transform WeaponPoint; //武器挂点
        protected Actor Actor;
        #endregion
        
        public virtual void Init()
        {
            AnimancerComponent = GetComponent<AnimancerComponent>();
        }
        #region 动画事件
        protected readonly Dictionary<string, Action> _animationEvents = new ();
        
        public void AnimationEvent(string eventName)
        {
            if(_animationEvents.TryGetValue(eventName, out Action action))
                action?.Invoke();
        }
        
        public void AddAnimationEvent(string eventName, Action action)
        {
            if (!_animationEvents.TryAdd(eventName, action))
                _animationEvents[eventName] += action;
        }

        public void RemoveAnimationEvent(string eventName, Action action)
        {
            if (_animationEvents.ContainsKey(eventName))
                _animationEvents[eventName] += action;
        }

        public void RemoveAnimationEvent(string eventName) => _animationEvents.Remove(eventName);
        
        public void ClearAllAnimationEvent() => _animationEvents.Clear();
        #endregion

        #region 音乐
        public void PlayAudio(string audioName, float value = 1f)
        {
            if(Actor.ActorConfig.AudioDic.TryGetValue(audioName, out var audioClip))
                AudioManager.Instance.PlayOnShot(audioClip, transform.position, value).Forget();
        }

        #endregion
        
        public void OnGameClose()
        {
            if (Actor.fsmMachine == null) return;
            Actor.fsmMachine.Destroy();
            Actor.fsmMachine = null;
        }
    }
}